/*
 * General main file for Towerhack Server
 */

#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "util/log.h"
#include "config/setup.h"
#include "common/common_main.h"

ApplicationState main_state;

void change_state (ApplicationState new_state)
{
	common_main_change_state (new_state);
}

void main_loop ()
{
	ticks = 0;
	while (main_state != STATE_QUIT) {
		common_main_update_state ();
		ticks--;
		while (ticks <= 0) {
			rest(1);
		}
	}
}

int main (int argc, char* argv[])
{
	main_state = STATE_INITIAL;
	if (argc != 1) log_print_usage_and_exit (argv[0], EXIT_FAILURE);

	log_level = LOG_DEBUG;
	log_init (true, LOGFILE_SERVER, true, true, true);

	// Timer from allegro? FIXME
	allegro_init();
	install_timer();
	install_int_ex(timer, BPS_TO_TIMER(FPS));
	
	load_data (); /* from files */
	all_init ();
	change_state (STATE_LOBBY); // initial default state

	main_loop ();

	cleanup ();
	log_info ("Towerhack Server shutdown");
	log_cleanup ();
	return EXIT_SUCCESS;
}
END_OF_MAIN()


#if TOWERHACK_DEBUG
void mouse_get_coords (int* buffer_x, int* buffer_y, int* map_x, int* map_y,
                       int* tile_x, int* tile_y, int* subtile)
{
	if (buffer_x != NULL) *buffer_x = 0;
	if (buffer_y != NULL) *buffer_y = 0;
	if (map_x != NULL)    *map_x = 0;
	if (map_y != NULL)    *map_y = 0;
	if (tile_x != NULL)   *tile_x = 0;
	if (tile_y != NULL)   *tile_y = 0;
	if (subtile != NULL)  *subtile = 0;
}
#endif
